package controller;

import de.javasoft.plaf.synthetica.SyntheticaWhiteVisionLookAndFeel;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Date;
import javax.swing.UIManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLHandler {

    DocumentBuilderFactory factory;
    DocumentBuilder builder;
    Document doc;
    TransformerFactory tf;
    Transformer t;
    Element session;
    Date d;
    File f;
    String friendUsername;
    

    public XMLHandler(String _friendUsername) {
        try {
            
            friendUsername = _friendUsername;
                        
            factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
            
            tf = TransformerFactory.newInstance();
            t = tf.newTransformer();
            f = new File(friendUsername);
            if (!f.exists()) {
                doc = builder.newDocument();
                session = doc.createElement("session");
                doc.appendChild(session);
            }
            else {
                doc=builder.parse(friendUsername);
                session = doc.getDocumentElement();
            }
            d = new Date();

        } catch (IOException | ParserConfigurationException | TransformerConfigurationException | DOMException | SAXException ex) {
            System.out.println("Error during creating / opening history file");
        }
    }

    public void saveChat(String fromUserName , String toUserName , String msg) {
        try {
            Element rootTag = doc.createElement("Message");
            session.appendChild(rootTag);

            Element from = doc.createElement("From");
            from.appendChild(doc.createCDATASection(fromUserName));
            rootTag.appendChild(from);

            Element to = doc.createElement("To");
            to.appendChild(doc.createCDATASection(toUserName));
            rootTag.appendChild(to);

            Element text = doc.createElement("Text");
            text.appendChild(doc.createCDATASection(msg));
            rootTag.appendChild(text);

            Element time = doc.createElement("Time");
            time.appendChild(doc.createCDATASection((d.toGMTString())));
            rootTag.appendChild(time);

            t.setOutputProperty(OutputKeys.INDENT, "yes");
            t.transform(new DOMSource(doc), new StreamResult(new File( friendUsername)));

            /*NodeList nl = doc.getElementsByTagName("rootTag");
            for (int i = 0; i < nl.getLength(); i++) {
                session.appendChild(nl.item(i));

            }*/

        } catch (IllegalArgumentException | TransformerException | DOMException ex) {
           // ex.printStackTrace();
            System.out.println("Error during creating / opening history file");
        }
    }
    
    

}
